package com.example.mobile2021606129ly0622;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btnMain6days;
    private Button btnMainLimit;
    private Button btnMainAqi;
    private Button btnMainAlert;
    private Button btnMainCondition;
    private TextView tvMainData;
 OkHttpClient okHttpClient =new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainCondition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           new Thread(new Runnable() {
               @Override
               public void run() {
                   RequestBody requestBody=new FormBody.Builder()
                           .add("cityID","917")
                           .add("token",MoJiDataManager.CONDITIONTOKEN)
                          .build();

                   Request request=new Request.Builder()
                           .addHeader("Authorization","APPCOME"+MoJiDataManager.AppCode)
                           .url(MoJiDataManager.CONDITIONURL)
                           .post(requestBody)
                           .build();

                   Call call=okHttpClient.newCall(request);
                   call.enqueue(new Callback() {
                       @Override
                       public void onFailure(@NotNull Call call, @NotNull IOException e) {

                       }

                       @Override
                       public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                           String result=response.body().string();
                           runOnUiThread(new Runnable() {
                               @Override
                               public void run() {
                                   tvMainData.setText(result);
                                   Log.i("TAG", "run: "+result);
                               }
                           });

                       }
                   });
               }
           }).start();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        btnMain6days = (Button) findViewById(R.id.btnMain6days);
        btnMainLimit = (Button) findViewById(R.id.btnMainLimit);
        btnMainAqi = (Button) findViewById(R.id.btnMainAqi);
        btnMainAlert = (Button) findViewById(R.id.btnMainAlert);
        btnMainCondition = (Button) findViewById(R.id.btnMainCondition);
        tvMainData = (TextView) findViewById(R.id.tvMainData);
    }
}